package com.qys.livesMall.shop.scheduledTask.processor.impl;

import cn.hutool.json.JSONObject;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.open.enums.PushMessageTypeEnum;
import com.qys.livesMall.open.modal.dto.app.PushUserDeviceContentDTO;
import com.qys.livesMall.open.modal.dto.mail.PushAllDeviceDTO;
import com.qys.livesMall.open.modal.dto.mail.PushTitleDTO;
import com.qys.livesMall.open.service.PushUserDeviceService;
import com.qys.livesMall.open.uniapp.Result;
import com.qys.livesMall.shop.constants.ShopOrderMessageConstants;
import com.qys.livesMall.shop.scheduledTask.processor.SeckillDelayTaskProcessor;
import com.qys.livesMall.shop.scheduledTask.taksEnum.TaskTypeEnum;
import com.qys.livesMall.shop.scheduledTask.task.SeckillTaks;
import jakarta.annotation.Resource;
import org.bson.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @packageName:
 * @className:SeckillDelayTaskProcessor
 * @Description:
 * @author:guomx
 * @date:2023/7/2915:02
 */
@Component
public class SeckillDelayTaskProcessorImpl implements SeckillDelayTaskProcessor {
    private final static Logger log = LoggerFactory.getLogger(SeckillDelayTaskProcessorImpl.class);
    @Resource
    private PushUserDeviceService pushUserDeviceService;
    @Override
    public TaskTypeEnum supportTask() {
        return TaskTypeEnum.Seckill;
    }

    @Override
    @Async
    public void process(SeckillTaks task) {
        String threadName = Thread.currentThread().getName();
        log.info("异步执行的线程名：" + threadName);
        try {
            PushAllDeviceDTO userDeviceDTO = new PushAllDeviceDTO();
            PushTitleDTO pushTitleDTO=new PushTitleDTO();
            pushTitleDTO.setChinese("秒杀");
            pushTitleDTO.setEnglish(ShopOrderMessageConstants.SECKILL);
            userDeviceDTO.setTitle(pushTitleDTO);
            PushUserDeviceContentDTO contentDTO = new PushUserDeviceContentDTO();
            contentDTO.setType(PushMessageTypeEnum.SECKILL_PUSH);
            JSONObject jsonObject=new JSONObject();
            jsonObject.putOnce("activityId",task.getActivityId());
            jsonObject.set("activityName",task.getActivityName());
            contentDTO.setData(jsonObject.toString());
            userDeviceDTO.setContent(contentDTO);
            Result pushFlag = pushUserDeviceService.pushAllUserDevice(userDeviceDTO);
        }catch (Exception e){
            log.error("Error executing seckill task: {}", e.getMessage());
        }
        log.info(threadName+"执行成功");
    }
}
